package com.demo2.one1.factory;

import com.demo2.one1.pojo.ActivityDO;
import com.demo2.one1.pojo.StockDO;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author SunChangSheng
 * @apiNote
 * @since 2021/12/23 15:11
 */
public class Factory {
    private static Map<Integer, ActivityDO> map = new HashMap<>();

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            ActivityDO activityDO = getActivityDo(1);
            activityDO.setNum(20 - i);
            System.out.println("商品id:" + activityDO.getId() + "，商品名称：" + activityDO.getName() + "，商品库存：" + activityDO.getNum());
        }
    }

    public static ActivityDO getActivityDo(Integer id) {
        ActivityDO activityDO = map.get(id);
        if (Objects.isNull(activityDO)) {
            activityDO = getActivityInfo();
            map.put(activityDO.getId(), activityDO);
        }
        return activityDO;
    }

    public static ActivityDO getActivityInfo() {
        ActivityDO activityDO = new ActivityDO();
        activityDO.setId(1);
        activityDO.setName("华为手机");
        activityDO.setStockId(1);
        return activityDO;
    }
}
